package com.xgame.server.netty;

import com.xgame.server.netty.coder.NettyMsgDecoder;
import com.xgame.server.netty.coder.NettyMsgEncoder;
import com.xgame.server.netty.coder.NettyMsgEncoder;
import com.xgame.server.netty.handler.ServerHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.LineBasedFrameDecoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
@Qualifier("springProtocolInitializer")
public class StringProtocolInitalizer  extends ChannelInitializer<SocketChannel> {

    @Autowired
    ServerHandler serverHandler;

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast("decoder", new NettyMsgDecoder());
        pipeline.addLast("encoder", new NettyMsgEncoder());
        pipeline.addLast("handler", serverHandler);
    }

    public void setServerHandler(ServerHandler serverHandler) {
        this.serverHandler = serverHandler;
    }
}
